<title>地址管理</title>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="seller-address-index-search_from">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">商家名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="title" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="seller-address-index-search_button">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <script type="text/html" id="seller-address-index-toolbar_tmp">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="seller-address-index-add">添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="seller-address-index-del">删除</button>
            </div>
        </script>
        <div class="layui-card-body">
            <table id="seller-address-index" lay-filter="seller-address-index"></table>
            <script type="text/html" id="seller-address-index-default_tmp">
                {{# if(d.default == 1){ }}
                <span class="layui-badge">是</span>
                {{# } else { }}
                <span class="layui-badge layui-bg-green">否</span>
                {{# } }}
            </script>
            <script type="text/html" id="seller-address-index-action_button">
                <a class="layui-btn layui-btn-xs" lay-event="seller-address-index-edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="seller-address-index-del">删除</a>
            </script>
        </div>
    </div>
</div>
<script>
    layui.use(['common', 'table'], function () {
        var $ = layui.jquery,
            common = layui.common,
            view = layui.view,
            table = layui.table,
            form = layui.form,
            lay_id_prefix = 'seller-address-index',//当前页id前缀
            edit_id_prefix = 'seller-address-add',//添加编辑页id前缀
            edit_view_url = 'seller/address/add',//添加编辑页模板地址
            api_action_url = layui.setter.apiHost + '/seller/address';

        //表单搜索
        form.on('submit(' + lay_id_prefix + '-search_button)', function (data) {
            var field = data.field;
            field.page = 1;//默认第一页
            table.reload(lay_id_prefix, {
                where: field
            });
        });

        //表格操作
        table.render({
            elem: '#' + lay_id_prefix,
            url: api_action_url,
            toolbar: '#' + lay_id_prefix + '-toolbar_tmp',
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID', width: 80},
                {field: 'title', title: '商家名称', minWidth: 120},
                {field: 'full_name', title: '收货人', minWidth: 100},
                {field: 'tel', title: '电话', minWidth: 150},
                {field: 'prov_name', title: '省份', minWidth: 100},
                {field: 'city_name', title: '城市', minWidth: 100},
                {field: 'area_name', title: '地区', minWidth: 100},
                {field: 'address', title: '地址', minWidth: 200},
                {field: 'default', title: '默认', width: 80, templet: '#' + lay_id_prefix + '-default_tmp'},
                {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#' + lay_id_prefix + '-action_button'}
            ]],
            page: true
        });

        //头部工具栏操作
        table.on('toolbar(' + lay_id_prefix + ')', function (obj) {
            var type = obj.event;
            switch (type) {
                case lay_id_prefix + '-add':
                    common.open_window('添加', api_action_url, edit_view_url, edit_id_prefix, {width: '100%'});
                    break;
                case lay_id_prefix + '-del':
                    layer.confirm('确定删除吗', function (index) {
                        common.ajax(api_action_url + '/delete', lay_id_prefix);
                    });
                    break;
            }
        });

        //监听工具条操作按钮
        table.on('tool(' + lay_id_prefix + ')', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case lay_id_prefix + '-edit':
                    common.open_window('编辑', api_action_url, edit_view_url, edit_id_prefix, {id: data.id, width: '100%'});
                    break;
                case lay_id_prefix + '-del':
                    layer.confirm('确定删除吗', function (index) {
                        if (common.ajax(api_action_url + '/delete', lay_id_prefix, {id: data.id}, false)) {
                            obj.del();
                            layer.close(index);
                        }
                    });
                    break;
            }
        });

        //监听锁定操作
        form.on('switch(' + lay_id_prefix + '-status_btn)', function (obj) {
            var send_data = {id: this.value, status: obj.elem.checked == true ? 1 : 0};
            common.ajax(api_action_url + '/status', lay_id_prefix, send_data, false);
        });

    });
</script>
